﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommonShop.Service.Helper;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Mapping.ByCode;
using CommonShop.Service;
using CommonShop.Web.Models;


namespace CommonShop.Web.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            if (Session["UserName"] != null) {
                return View();
            }
            else {
                return RedirectToAction("Login", "User");
            }
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(RegisterModel collection)
        {
            if (ModelState.IsValid) {
                try {
                    CommonShop.Service.User user = new CommonShop.Service.User();

                    var factory = FluentSessionFactory.GetCurrentFactory();
                    using (var session = factory.OpenSession())
                    {
                        //IList<User> userList = session.CreateQuery("from User").List<User>();
                        user.UserID = Guid.NewGuid();
                        user.UserName = collection.UserName;
                        user.UserPassword = collection.Password;
                        user.Email = collection.Email;
                        //todo
                        user.UserState = "test";
                        //IList<Role> roleList = session.CreateQuery(string.Format(@"from Role where RoleId ='{0}'",Constants.Admin)).List<Role>();
                        Role role = session.QueryOver<Role>().Where(C => C.RoleID == Constants.Admin).SingleOrDefault();
                        user.Role = role;
                        session.Save(user);
                        session.Flush();
                        Session["UserName"] = collection.UserName;
                        return RedirectToAction("Index");
                    }
                    //NHibernateHelper helper = new NHibernateHelper();
                    //ISession session = helper.GetSession();

                    //session.Save(collection);
                    //session.Flush();
                    //Session["UserName"] = collection.UserName;
                    //return RedirectToAction("Index");

                }
                catch {
                    return View();
                }
            }
            return View(collection);
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Login() {
            return View();
        }

        [HttpPost]
        public ActionResult Login(User collection) {
            NHibernateHelper helper = new NHibernateHelper();
            ISession session = helper.GetSession();
            IList<User> userList =  session.CreateQuery(string.Format(@"from User where UserName='{0}' and UserPassword = '{1}'", collection.UserName, collection.UserPassword)).List<User>();

            if (userList.Count > 0) {
                Session["UserName"] = userList[0].UserName;
                if (userList[0].Role.RoleID == Constants.Admin) {
                    return RedirectToAction("Main_Index", "Admin");
                }
                return RedirectToAction("Index");
            }
            else {
                ViewBag.ErrorMsg = "登陆失败";
            }
            return View();
            
        }
    }
}
